<?php
/**
 * Description: guest账号生成
 * User: devforma
 * Date: 15/1/4
 * Time: 11:45
 */
class GuestRegister implements iController {
    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb, $cmsdb;
        
        //参数校验
        if (!isset($queryArgs['uuid']) || !validateUUID($queryArgs['uuid'])) {
            return genOutput(1000); //参数错误
        }
        
        $guestInfo = $uddb->getRow("SELECT * FROM `{$uddb->guestInfo}` WHERE `uuid`=:uuid",
            array(':uuid' => $queryArgs['uuid']));

        if (!empty($guestInfo)) {
            
            //获取游客的考试信息
            $sql = "SELECT `testarea_id`, `user_sno` FROM `{$uddb->userDPData}` WHERE `guest_id`={$guestInfo->id}";
            $dailyPlan = $uddb->getRow($sql);
            if (!empty($dailyPlan)) {
                $sql = "SELECT * FROM `data_exam_detail` AS ed, `data_exams` AS e WHERE ed.exam_id=e.exam_id AND ed.id={$dailyPlan->testarea_id} LIMIT 1";
                $testInfo = $cmsdb->getRow($sql);
                
                $exam = array(
                    'exam_id' => intval($testInfo->id),
                    'name' => $testInfo->exam_name,
                    'code' => $testInfo->shortening,
                    'date' => $testInfo->exam_day,
                    'is_official' => $testInfo->is_official == '1'
                );
                $sno = $dailyPlan->user_sno;
            } else {
                $exam = new stdClass();
                $sno = 0;
            }

            return genOutput(1, array(
                'guest_info' => array(
                    'guest_id' => intval($guestInfo->id),
                    'uuid' => $guestInfo->uuid,
                    'token' => $guestInfo->guest_token,
                    'sno' => intval($sno)
                ),
                'exam' => $exam
            ));
        }

        //guest_info表插入记录
        $uddb->insert($uddb->guestInfo, array(
            'uuid' => $queryArgs['uuid'],
            'user_id' => 0,
            'guest_token' => ''
        ));
        $guestId = $uddb->lastInsertId();

        //自动设置考试项目为最新的国考
        $exams = getAvailableExams();
        $url = sprintf(TWINKLE_URL . '/dailyplan/set_exam?id=%d&user_id=%d&is_guest=%d',
            1126,
            $guestId,
            true
        );
        
        $resp = doCurl($url);
        $sno = 0;
        if (($data = json_decode($resp)) && $data->response_code == 1) {
            $sno = $data->sno;
        }

        //生成并更新user_token
        $token = md5($guestId.':'.ENCRYPTION_KEY);
        $uddb->update($uddb->guestInfo, array('guest_token' => $token), array('id' => $guestId));

        //缓存用户信息
        cacheSet('guest:'.$guestId, array(
            'uuid' => $queryArgs['uuid'],
            'token' => $token
        ));

        return genOutput(1, array(
            'guest_info' => array(
                'guest_id' => intval($guestId),
                'uuid' => $queryArgs['uuid'],
                'token' => $token,
                "sno" => $sno
            ),
            'exam' => $exams[0]
        ));
    }
}